/**
 * Created by parth.upadhye on 21/07/2014.
 */

function partialPath()
{
    return "partials/";
}

var topicApp = angular.module("topicApp", [
    'ngRoute', 'topicApp'
]);

var userApp = angular.module("userApp", [
    'ngRoute', 'topicDirectives'
]);
userApp.controller("LoginController", ['$scope', '$routeParams', '$http', function ($scope, $routeParams, $http) {
    BaseController ( $scope );
}]);

userApp.controller("TopicsController", ['$scope', '$routeParams', '$http',
    function ($scope, $routeParams, $http) {

        BaseController ( $scope );

        $scope.topicBeingEdited = false;

        var array = $routeParams.params.split("&&");
        $scope.selectedCategory = "";
        if ( array[0] != ""  ) {
            selectedCategoryUuid = array[0];
        }
        for (var c = 0; c < categories.length; c++)
        {
            if ( categories[c].uuid == selectedCategoryUuid )
            {
                $scope.selectedCategory = categories[c].name;
            }
        }
        if ( array[1] != "" )
        {
            if ( array[1] == "undefined" || array[1] == "")
            {
                topicSearchTags = "";
            } else {
                topicSearchTags = array[1].toUpperCase()
            }
            $scope.searchTagText = topicSearchTags;
        } else {
            if ( topicSearchTags == undefined || topicSearchTags == "" )
            {
                $scope.searchTagText = "";
            } else {
                $scope.searchTagText = topicSearchTags;
            }
        }
        $scope.topicUuid = array[2];
        $scope.topicUncommitted = null;

        $scope.tagsInCategory = [];
        $scope.selectedTags="";

        $scope.searchThisTag = function ( tag ) {
            var tags;
            if ($scope.selectedTags == "" ) {
                tags = [];
            } else {
                tags = $scope.selectedTags.split(", ");
            }
            var addTag = true;
            for (var t = 0; t < tags.length; t++) {
                if (tags[t] == tag) {
                    tags.splice(t, 1);
                    $scope.selectedTags = tags.join(", ");
                    return;
                }
            }
            if (addTag) {
                tags.push(tag);
            }
            if (tags.length == 0)
            {
                $scope.selectedTags = "";
            } else {
                $scope.selectedTags = tags.join(", ");
            }
        }

        $scope.getTagsInCategory = function ()
        {
            var category = $scope.selectedCategory;

            $http.get('data/creator/tagsInCategory.json')
                .success(function(data) {
                    $scope.tagsInCategory = data;
                }
            );
        }

        $scope.getTagsInCategory ();

        $scope.topics=[];
        $scope.topicsList=[];

        $scope.showTopic = function ()
        {
            if ( $scope.topicUuid == undefined || $scope.topicUuid.length == 0 || $scope.topicUuid == "undefined")
            {
                $scope.topicBeingEdited = false;

            } else {
                $scope.topicBeingEdited = true;

                $http.get('data/topic.json')
                    .success(function(data) {
                        $scope.topic = data;
                        $scope.topicUncommitted = angular.copy( $scope.topic );
                    }
                );
            }
        }

        $scope.getTopicsForTags = function ( selectedCategory, searchTags )
        {
            $http.get('data/userTopicsSearchList.json')
                .success(function(data) {
                    var obj = data;
                    $scope.topicsList = data;
                    $scope.getTopicsSearchPage();
                    //
                }
            );
        }
        $scope.getTopicFromList = function ( tid )
        {
            $scope.topicUuid = tid;
            $scope.showTopic();
        }

        $scope.getTopicsSearchPage = function ()
        {
            $scope.showTopicBeingEdited = false;
            $scope.showTopicsSearch = true;
        }

        $scope.getTopicEditorPage = function ()
        {
            $scope.showTopicBeingEdited = true;
            $scope.showTopicsSearch = false;
            $scope.$apply();
        }

    }]);


userApp.controller("HomeController", ['$scope', '$routeParams', '$location', '$http',
    function ($scope, $routeParams, $location, $http) {

        BaseController ( $scope );

        if ($routeParams.userUuid != "")
        {
            userUuid = $routeParams.userUuid;
        }

        $scope.tagCloudMin = 0;
        $scope.tagCloudMax = 0;
        $scope.tagCloudStep = 0;

        $http.get('http://localhost:3001/GetUserTopicsInCategories?user='+userUuid)
            .success(function(data) {
                createBarCloud( userTopicsCloudCanvas, data ,null );
            }
        );

        $http.get('http://localhost:3001/GetTopicsInCategories')
            .success(function(data) {
                createBarCloud( availableCategoryTopicsCloudCanvas, data ,null );
            }
        );

    }]);

userApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/home/:userUuid', {
                templateUrl: partialPath() + 'user/userHome.html',
                controller: 'HomeController'
            }).
            when('/topics/:params', {
                templateUrl: partialPath() + 'user/userTopics.html',
                controller: 'TopicsController'
            }).
            otherwise({
                templateUrl: partialPath() + 'user/userLogin.html',
                controller: 'LoginController'
            });
    }]);